﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace ChineseRemainderTheorem.Mvvm
{
    public class SimpleCommand : ICommand
    {
        #region Fields

        private readonly Action<object> _execute;

        private readonly Func<object, bool> _canExecute; 

        #endregion

        #region Constructors

        public SimpleCommand(Action<object> execute, Func<object, bool> canExecute)
        {
            if (ReferenceEquals(execute, null))
            {
                throw new ArgumentNullException("execute");
            }
            _execute = execute;

            if (ReferenceEquals(canExecute, null))
            {
                throw new ArgumentNullException("canExecute");
            }
            _canExecute = canExecute;
        }

        #endregion

        public bool CanExecute(object parameter)
        {
            return _canExecute(parameter);
        }

        public void Execute(object parameter)
        {
            _execute(parameter);
        }

        public event EventHandler CanExecuteChanged
        {
            add
            {
                if (!ReferenceEquals(value, null))
                {
                    CommandManager.RequerySuggested += value;
                }
            }
            remove
            {
                if (!ReferenceEquals(value, null))
                {
                    CommandManager.RequerySuggested -= value;
                }
            }
        }
    }
}
